package com.ben.multiple.schedule;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author BenJin Yin
 * @date 2021/7/30
 */
@Component
public class Test implements ApplicationContextAware, InitializingBean {
    ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ScheduledAnnotationBeanPostProcessor postProcessor = applicationContext.getBean(ScheduledAnnotationBeanPostProcessor.class);
        Set<ScheduledTask> scheduledTasks = postProcessor.getScheduledTasks();
        scheduledTasks.forEach(System.out::println);
    }
}
